package com.tjcd.project.library_base.common.utils;

import android.content.Context;
import android.text.InputFilter;
import android.text.Spanned;
import android.widget.EditText;

import com.blankj.utilcode.util.ToastUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author jiangKunKun
 * @CreateDate 2021/7/12
 * @Description 输入框的限制
 * @SpittingGrooves
 */
public class EditTextUtil {

    private static InputFilter sInputFilter;

    /**
     *  禁止输入表情
     * @param editText 输入框对象
     * @param length 长度
     */
    public static void prohibitInputExpression(final Context context, EditText editText,int length){
        sInputFilter = new InputFilter() {
            Pattern emoji = Pattern.compile("[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]", Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);

            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                Matcher emojiMatcher = emoji.matcher(source);
                if (emojiMatcher.find()) {
                    ToastUtils.showLong("不可以输入表情");
                    return "";
                }
                return null;
            }
        };
        // 禁止输入表情以及限制输入长度为10
        editText.setFilters(new InputFilter[]{sInputFilter, new InputFilter.LengthFilter(length)});

    }

    /**
     *  禁止输入表情和特殊符号
     * @param editText 输入框对象
     * @param length 长度
     */
    public static void prohibitInputExpressionAndsymbol(EditText editText, int length){
        sInputFilter = new InputFilter() {
            Pattern emoji = Pattern.compile("[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]|[`~!@#_$%^&*()+=|{}':;',\\\\[\\\\].<>/?~！@#￥%……&*（） +|{}【】‘；：”“’。，、？]", Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);

            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend){
                Matcher emojiMatcher = emoji.matcher(source);
                if (emojiMatcher.find()){
                    ToastUtils.showLong("不能输入特殊符号");
                    return "";
                }
                return null;
            }
        };
        // 禁止输入表情以及限制输入长度为10
        editText.setFilters(new InputFilter[]{sInputFilter, new InputFilter.LengthFilter(length)});

    }

    /**
     * EditText 和 横向滚动冲突
     * EditText竖直方向是否可以滚动
     * @param editText  需要判断的EditText
     * @return  true：可以滚动   false：不可以滚动
     */
    public static boolean canVerticalScroll(EditText editText) {
        //滚动的距离
        int scrollY = editText.getScrollY();
        //控件内容的总高度
        int scrollRange = editText.getLayout().getHeight();
        //控件实际显示的高度
        int scrollExtent = editText.getHeight() - editText.getCompoundPaddingTop() -editText.getCompoundPaddingBottom();
        //控件内容总高度与实际显示高度的差值
        int scrollDifference = scrollRange - scrollExtent;

        if(scrollDifference == 0) {
            return false;
        }

        return (scrollY > 0) || (scrollY < scrollDifference - 1);
    }
    /**
     * mPigeonAddMsgEt.setOnTouchListener(new View.OnTouchListener() {
     *             @Override
     *             public boolean onTouch(View v, MotionEvent event){
     *                 //触摸的是EditText并且当前EditText可以滚动则将事件交给EditText处理；否则将事件交由其父类处理
     *                 if (EditTextUtil.canVerticalScroll(mPigeonAddMsgEt)){
     *                     v.getParent().requestDisallowInterceptTouchEvent(true);
     *                     if (event.getAction() == MotionEvent.ACTION_UP){
     *                         v.getParent().requestDisallowInterceptTouchEvent(false);
     *                     }
     *                 }
     *                 return false;
     *             }
     *         });
     */
}
